from rest_framework.serializers import *
from apps.orders.models import OrderInfo, OrderGoods, SKU


# 再次嵌套获取sku信息的名字和图片
class OrderGoodsSKUSerializer(ModelSerializer):
    class Meta:
        model = SKU
        fields = ['name', 'default_image']


# 序列化器嵌套获取商品信息
class OrderGoodsSerializer(ModelSerializer):
    sku = OrderGoodsSKUSerializer()

    # 此时没有默认图片 获取商品名称 以下代码就可以
    # sku = StringRelatedField()

    class Meta:
        model = OrderGoods  # 图片，商品名称，价格，数量
        fields = '__all__'


# 显示订单列表
class OrderInfoSerializer(ModelSerializer):
    # AssertionError: The field 'skus' was declared on serializer OrderInfoSerializer,
    # but has not been included in the 'fields' option.
    skus = OrderGoodsSerializer(many=True)

    class Meta:
        model = OrderInfo
        # 详情显示 还是需要更多数据的
        fields = '__all__'
        # 以下代码是列表显示
        # fields = ['order_id','create_time','skus']
